package com.casems.service.impl;


import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.Common;
import com.casems.commons.execption.ParamExecption;
import com.casems.service.SMSService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SMSServiceImpl implements SMSService {

    @Value("${alicloud.access-key}")
    private String accessKey;

    @Value("${alicloud.access-key-secret}")
    private String secretAccessKey;

    @Value("${alicloud.sign-name}")
    private String signName;

    @Value("${alicloud.template-code}")
    private String templateCode;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKey)
                .setAccessKeySecret(secretAccessKey);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    @Override
    public boolean sendCode(String phone) throws Exception {
        try {
            String code = String.valueOf((int)(Math.random()*900000)+100000);
            stringRedisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

            Map<String,String> params = new HashMap<String,String>();
            params.put("code",code);
            String paramstr = JSONUtil.toJsonStr(params);
            com.aliyun.dysmsapi20170525.Client client = createClient();
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setSignName(signName)
                    .setTemplateCode(templateCode)
                    .setPhoneNumbers(phone)
                    .setTemplateParam(paramstr);
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();

            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            if(!"ok".equalsIgnoreCase(sendSmsResponse.body.message)){
                throw new ParamExecption(sendSmsResponse.body.message,500);
            }

        } catch (TeaException error) {
            String s = Common.assertAsString(error.message);
            System.out.println(s);
            return false;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            System.out.println(error.message);
            return false;
        }
        return true;
    }
}
